package com.gao.f_reactor.d.server;

import java.io.IOException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @author gao
 * @time 2020/08/23 18:14:27
 */
public class Acceptor implements Runnable {

    private ServerSocketChannel serverSocketChannel;
    private SubReactor subReactor = new SubReactor();

    public Acceptor(ServerSocketChannel serverSocketChannel) {
        this.serverSocketChannel = serverSocketChannel;
        // 异步启动SubReactor
        new Thread(subReactor).start();
    }

    @Override
    public void run() {
        try {
            SocketChannel channel = serverSocketChannel.accept();
            System.out.println("有客户端连接：" + channel.getRemoteAddress());
            subReactor.register(channel);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
